package org.dxy.infrastructure.persistent.db.mybatispuls.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dxy.infrastructure.persistent.db.mybatispuls.po.UnitManagePo;
import org.dxy.infrastructure.persistent.db.mybatispuls.mapper.UnitManageMapper;
import org.dxy.infrastructure.persistent.db.mybatispuls.service.UnitManageService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @description: 单位管理Service实现类
 * @author: dxy
 * @createTime: 2025/1/21
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class UnitManageServiceImpl extends ServiceImpl<UnitManageMapper, UnitManagePo> implements UnitManageService {

    private final UnitManageMapper unitManageMapper;

    @Override
    public UnitManagePo getByUnitCode(String unitCode) {
        if (!StringUtils.hasText(unitCode)) {
            return null;
        }
        LambdaQueryWrapper<UnitManagePo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UnitManagePo::getUnitCode, unitCode);
        return getOne(wrapper);
    }

    @Override
    public List<UnitManagePo> listByUnitType(String unitType) {
        if (!StringUtils.hasText(unitType)) {
            return list();
        }
        LambdaQueryWrapper<UnitManagePo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UnitManagePo::getUnitType, unitType);
        return list(wrapper);
    }

    @Override
    public List<UnitManagePo> listByStatus(Integer status) {
        if (status == null) {
            return list();
        }
        LambdaQueryWrapper<UnitManagePo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UnitManagePo::getStatus, status);
        return list(wrapper);
    }

    @Override
    public List<UnitManagePo> listAllEnabled() {
        LambdaQueryWrapper<UnitManagePo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UnitManagePo::getStatus, 1)
               .orderByAsc(UnitManagePo::getId);
        return list(wrapper);
    }

    @Override
    public List<UnitManagePo> listByBaseUnitId(Long baseUnitId) {
        if (baseUnitId == null) {
            return list();
        }
        LambdaQueryWrapper<UnitManagePo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UnitManagePo::getBaseUnitId, baseUnitId);
        return list(wrapper);
    }

    @Override
    public Page<UnitManagePo> pageQuery(Page<UnitManagePo> page, UnitManagePo query) {
        LambdaQueryWrapper<UnitManagePo> wrapper = new LambdaQueryWrapper<>();
        
        if (query != null) {
            // 单位编码模糊查询
            if (StringUtils.hasText(query.getUnitCode())) {
                wrapper.like(UnitManagePo::getUnitCode, query.getUnitCode());
            }
            
            // 单位名称模糊查询
            if (StringUtils.hasText(query.getUnitName())) {
                wrapper.like(UnitManagePo::getUnitName, query.getUnitName());
            }
            
            // 单位类型精确查询
            if (StringUtils.hasText(query.getUnitType())) {
                wrapper.eq(UnitManagePo::getUnitType, query.getUnitType());
            }
            
            // 状态精确查询
            if (query.getStatus() != null) {
                wrapper.eq(UnitManagePo::getStatus, query.getStatus());
            }
            
            // 基本单位ID精确查询
            if (query.getBaseUnitId() != null) {
                wrapper.eq(UnitManagePo::getBaseUnitId, query.getBaseUnitId());
            }
        }
        
        // 按ID排序
        wrapper.orderByAsc(UnitManagePo::getId);
        
        return page(page, wrapper);
    }

    @Override
    public boolean existsByUnitCode(String unitCode, Long excludeId) {
        if (!StringUtils.hasText(unitCode)) {
            return false;
        }

        LambdaQueryWrapper<UnitManagePo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UnitManagePo::getUnitCode, unitCode);
        
        if (excludeId != null) {
            wrapper.ne(UnitManagePo::getId, excludeId);
        }
        
        return count(wrapper) > 0;
    }

    @Override
    public boolean batchDelete(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return false;
        }
        
        try {
            // 检查是否有其他单位依赖这些单位作为基本单位
            for (Long id : ids) {
                List<UnitManagePo> dependentUnits = listByBaseUnitId(id);
                if (!dependentUnits.isEmpty()) {
                    log.warn("单位ID {} 被其他单位作为基本单位引用，不能删除", id);
                    return false;
                }
            }
            
            return removeBatchByIds(ids);
        } catch (Exception e) {
            log.error("批量删除单位失败", e);
            return false;
        }
    }

    @Override
    public List<UnitManagePo> listBaseUnits() {
        LambdaQueryWrapper<UnitManagePo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UnitManagePo::getIsBaseUnit, 1)
               .eq(UnitManagePo::getStatus, 1)
               .orderByAsc(UnitManagePo::getId);
        return list(wrapper);
    }

    @Override
    public List<UnitManagePo> listBaseUnitsByType(String unitType) {
        LambdaQueryWrapper<UnitManagePo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UnitManagePo::getIsBaseUnit, 1)
               .eq(UnitManagePo::getStatus, 1);
        
        if (StringUtils.hasText(unitType)) {
            wrapper.eq(UnitManagePo::getUnitType, unitType);
        }
        
        wrapper.orderByAsc(UnitManagePo::getId);
        return list(wrapper);
    }
}